
import gradio as gr
import random
import time
import requests
import json

def responds(prompt):
    url = 'http://171.60.1.112:1025/generate'

    json_data = {
        "prompt": prompt,
        "max_tokens": 1280000,
        "repetition_penalty": 1.03,
        "presence_penalty": 1.2,
        "frequency_penalty": 1.2,
        "temperature": 0.001,
        "top_k": int(10),
        "top_p": 0.95,
        "stream": False
    }
    # 发送POST请求
    response = requests.post(url, json=json_data)
    context = response.text
    # context = json.loads(context)
    # context = context["text"][0]
    # print("\n\n")

    # sys.stdout.write(context + '\n')
    return context


with gr.Blocks() as demo:
    chatbot = gr.Chatbot()
    msg = gr.Textbox()
    clear = gr.Button("清除")

    def respond(message, chat_history):
        #bot_message = random.choice(["你好吗？", "我爱你", "我很饿"])
        bot_message=responds(message)
        chat_history.append((message, bot_message))
        time.sleep(1)
        return "", chat_history

    msg.submit(respond, [msg, chatbot], [msg, chatbot])
    clear.click(lambda: None, None, chatbot, queue=False)

demo.launch(server_name="168.17.100.17")
